﻿using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.ViewModels.Controls;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace Jn698._45GWTool.ViewModels
{
    public class MeterListViewModel:ViewModelBase
    {
        public static MSDataModel mSDataModel;
        private ObservableCollection<MSData> values;

        public ObservableCollection<MSData> Values
        {
            get { return values; }
            set { Set(ref values, value); }
        }
        private bool userTypeVisibility;

        public bool UserTypeVisibility
        {
            get { return userTypeVisibility; }
            set { Set(ref userTypeVisibility, value); }
        }

        private bool userAddressVisibility;

        public bool UserAddressVisibility
        {
            get { return userAddressVisibility; }
            set { Set(ref userAddressVisibility, value); }
        }

        private bool configNumVisibility;

        public bool ConfigNumVisibility
        {
            get { return configNumVisibility; }
            set { Set(ref configNumVisibility, value); }
        }

        private bool userTypeRegionVisibility;

        public bool UserTypeRegionVisibility
        {
            get { return userTypeRegionVisibility; }
            set { Set(ref userTypeRegionVisibility, value); }
        }


        private ObservableCollection<RSD2Data> rsd2DataList;

        public ObservableCollection<RSD2Data> RSD2DataList
        {
            get { return rsd2DataList; }
            set { Set(ref rsd2DataList, value); }
        }
        #region ms
        private List<string> msList;

        public List<string> MSList
        {
            get { return msList; }
            set { Set(ref msList, value); }
        }

        private int selectedMS;

        public int SelectedMS
        {
            get { return selectedMS; }
            set { Set(ref selectedMS, value); SetZIndex(); }
        }


        private int selectedRSD4MS;

        public int SelectedRSD4MS
        {
            get { return selectedRSD4MS; }
            set { Set(ref selectedRSD4MS, value); }
        }

        private int selectedRSD5MS;

        public int SelectedRSD5MS
        {
            get { return selectedRSD5MS; }
            set { Set(ref selectedRSD5MS, value); }
        }

        private ObservableCollection<MSRegionData> msRegionDataList;

        public ObservableCollection<MSRegionData> MSRegionDataList
        {
            get { return msRegionDataList; }
            set { Set(ref msRegionDataList, value); }
        }

        private List<SingleData> unitList;

        public List<SingleData> UnitList
        {
            get { return unitList; }
            set { unitList = value; }
        }

        #endregion
        
        public MeterListViewModel()
        {
            Values = new ObservableCollection<MSData>() { };
            MSList = new List<string>() { "无电能表", "全部用户地址", "一组用户类型", "一组用户地址", "一组配置序号", "一组用户类型区间", "一组用户地址区间", "一组配置序号区间" };
            MSRegionDataList = new ObservableCollection<MSRegionData>();
            UnitList = new List<SingleData>() {
                new SingleData { Name = "前闭后开" }, new SingleData { Name = "前开后闭" },
                new SingleData { Name = "前闭后闭" } ,new SingleData { Name = "前开后开" }
            };
            //string MSData = ServiceLocator.Current.GetInstance<AddOrUpdateConnectPlanViewModel>().MSData;
            try
            {
                mSDataModel = ServiceLocator.Current.GetInstance<MSDataModel>();
                string MSData = mSDataModel.Data;
                if (MSData?.Length >= 2)
                {
                    SelectedMS = int.Parse(MSData.Substring(0, 2));
                    MSInit(SelectedMS, MSData.Substring(0));
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            
           
        }

        /// <summary>
        /// 切换参数表格
        /// </summary>
        /// <param name="zIndexName"></param>
        private void SetZIndex()
        {
            Values.Clear();
            MSRegionDataList.Clear();
            UserTypeVisibility = false;
            UserAddressVisibility = false;
            ConfigNumVisibility = false;
            UserTypeRegionVisibility = false;
            switch (SelectedMS)
            {
                case 2:
                    UserTypeVisibility = true;
                    break;
                case 3:
                    UserAddressVisibility = true;
                    break;
                case 4:
                    ConfigNumVisibility = true;
                    break;
                case 5:
                case 6:
                case 7:
                    UserTypeRegionVisibility = true;
                    break;
                case 0:
                case 1:
                default:
                    break;
            }
        }

        private void MSInit(int index, string MSData)
        {
            int point;
            SetZIndex();
            switch (index)
            {
                case 4:
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {
                        MSData mSData = new MSData { data = Convert.ToByte(MSData.Substring(4 + 4 * i, 4), 16).ToString() };
                        Values.Add(mSData);
                    }
                    break;
                case 2:
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {
                        MSData mSData = new MSData { data = Convert.ToByte(MSData.Substring(4 + 2 * i, 2), 16).ToString() };
                        Values.Add(mSData);
                    }
                    break;
                case 3:
                    point = 4;
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {

                        int TSAlen = int.Parse(MSData.Substring(point, 2));
                        MSData mSData = new MSData { data = MSData.Substring(point + 4, (TSAlen - 1) * 2) };
                        Values.Add(mSData);
                        point += 2 + TSAlen * 2;
                    }
                    break;
                case 5:
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {
                        MSRegionData mSRegionData = new MSRegionData
                        {
                            Unit = GetMSRegionUnit(int.Parse(MSData.Substring(4 + 6 * i, 2))),
                            Start = Convert.ToInt16(MSData.Substring(6 + 6 * i, 2), 16).ToString(),
                            End = Convert.ToInt16(MSData.Substring(8 + 6 * i, 2), 16).ToString()
                        };
                        MSRegionDataList.Add(mSRegionData);
                    }
                    break;
                case 6:
                    point = 4;
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {
                        string startData = "";
                        string endData = "";
                        string unitData = GetMSRegionUnit(int.Parse(MSData.Substring(point, 2)));
                        point += 2;
                        int TSAlen = int.Parse(MSData.Substring(point, 2));
                        startData = MSData.Substring(point + 4, (TSAlen - 1) * 2);
                        point += 2 + TSAlen * 2;
                        TSAlen = int.Parse(MSData.Substring(point, 2));
                        endData = MSData.Substring(point + 4, (TSAlen - 1) * 2);
                        point += 2 + TSAlen * 2;
                        MSRegionData mSRegionData = new MSRegionData
                        {
                            Unit = unitData,
                            Start = startData,
                            End = endData
                        };

                        MSRegionDataList.Add(mSRegionData);
                    }
                    break;
                case 7:
                    for (int i = 0; i < int.Parse(MSData.Substring(2, 2)); i++)
                    {
                        MSRegionData mSRegionData = new MSRegionData
                        {
                            Unit = GetMSRegionUnit(int.Parse(MSData.Substring(4 + 10 * i, 2))),
                            Start = Convert.ToInt16(MSData.Substring(6 + 10 * i, 4), 16).ToString(),
                            End = Convert.ToInt16(MSData.Substring(10 + 10 * i, 4), 16).ToString()
                        };
                        MSRegionDataList.Add(mSRegionData);
                    }
                    break;
                case 0:
                case 1:
                default:
                    break;
            }
        }
        public string GetMSInfo()
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append(SelectedMS.ToString("X2"));
            switch (SelectedMS)
            {
                case 4:
                    stringBuilder.Append(Values.Count.ToString("X2"));
                    for (int i = 0; i < Values.Count; i++)
                    {
                        MSData mSData = Values[i];
                        try
                        {
                            stringBuilder.Append(int.Parse(mSData.data).ToString("X4"));
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                            break;
                        }

                    }
                    break;
                case 2:
                    stringBuilder.Append(Values.Count.ToString("X2"));
                    for (int i = 0; i < Values.Count; i++)
                    {
                        MSData mSData = Values[i];
                        try
                        {
                            stringBuilder.Append(int.Parse(mSData.data).ToString("X2"));
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                            break;
                        }

                    }
                    break;
                case 3:
                    stringBuilder.Append(Values.Count.ToString("X2"));
                    for (int i = 0; i < Values.Count; i++)
                    {
                        MSData mSData = Values[i];
                        try
                        {
                            if (mSData.data.Length % 2 != 0)
                            {
                                mSData.data = mSData.data.PadLeft(mSData.data.Length + 1, '0');
                            }
                            stringBuilder.Append((mSData.data.Length / 2 + 1).ToString("X2"));
                            stringBuilder.Append((mSData.data.Length / 2 + -1).ToString("X2"));
                            stringBuilder.Append(mSData.data);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                            break;
                        }

                    }
                    break;
                case 5:
                    stringBuilder.Append(MSRegionDataList.Count.ToString("X2"));
                    for (int i = 0; i < MSRegionDataList.Count; i++)
                    {
                        MSRegionData mSRegionData = MSRegionDataList[i];

                        switch (mSRegionData.Unit)
                        {
                            case "前闭后开":
                                stringBuilder.Append("00");
                                break;
                            case "前开后闭":
                                stringBuilder.Append("01");
                                break;
                            case "前闭后闭":
                                stringBuilder.Append("02");
                                break;
                            case "前开后开":
                                stringBuilder.Append("03");
                                break;
                            default:
                                break;
                        }
                        stringBuilder.Append(int.Parse(mSRegionData.Start).ToString("X2"));
                        stringBuilder.Append(int.Parse(mSRegionData.End).ToString("X2"));
                    }
                    break;
                case 6:
                    stringBuilder.Append(MSRegionDataList.Count.ToString("X2"));
                    for (int i = 0; i < MSRegionDataList.Count; i++)
                    {
                        MSRegionData mSRegionData = MSRegionDataList[i];

                        switch (mSRegionData.Unit)
                        {
                            case "前闭后开":
                                stringBuilder.Append("00");
                                break;
                            case "前开后闭":
                                stringBuilder.Append("01");
                                break;
                            case "前闭后闭":
                                stringBuilder.Append("02");
                                break;
                            case "前开后开":
                                stringBuilder.Append("03");
                                break;
                            default:
                                break;
                        }
                        stringBuilder.Append((mSRegionData.Start.Length / 2 + 1).ToString("X2"));
                        stringBuilder.Append((mSRegionData.Start.Length / 2 + -1).ToString("X2"));
                        stringBuilder.Append(mSRegionData.Start);
                        stringBuilder.Append((mSRegionData.End.Length / 2 + 1).ToString("X2"));
                        stringBuilder.Append((mSRegionData.End.Length / 2 + -1).ToString("X2"));
                        stringBuilder.Append(mSRegionData.End);

                    }
                    break;
                case 7:
                    stringBuilder.Append(MSRegionDataList.Count.ToString("X2"));
                    for (int i = 0; i < MSRegionDataList.Count; i++)
                    {
                        MSRegionData mSRegionData = MSRegionDataList[i];

                        switch (mSRegionData.Unit)
                        {
                            case "前闭后开":
                                stringBuilder.Append("00");
                                break;
                            case "前开后闭":
                                stringBuilder.Append("01");
                                break;
                            case "前闭后闭":
                                stringBuilder.Append("02");
                                break;
                            case "前开后开":
                                stringBuilder.Append("03");
                                break;
                            default:
                                break;
                        }
                        stringBuilder.Append(int.Parse(mSRegionData.Start).ToString("X4"));
                        stringBuilder.Append(int.Parse(mSRegionData.End).ToString("X4"));
                    }
                    break;
                case 0:
                case 1:
                default:
                    break;
            }
            return stringBuilder.ToString();
        }
        private string GetMSRegionUnit(int index)
        {
            switch (index)
            {
                case 0:
                    return "前闭后开";
                case 1: return "前开后闭";
                case 2: return "前闭后闭";
                case 3: return "前开后开";
                default:
                    return "";
            }
        }

        public RelayCommand<Window> OKCommand
        {
            get
            {
                var command = new RelayCommand<Window>((w) =>
                {
                    mSDataModel.Data = GetMSInfo();
                    w.DialogResult = true;
                });
                return command;
            }
        }
    }
}
